<template>
	<view class="pages">
		<view class="pad-t-100 mar-x-28 font-22 font-b-800">
			<text class="dis-block">你好，</text>
			<text class="dis-block mar-t-10">欢迎来到售后宝！</text>
		</view>
		
		<view class="mar-t-100 mar-x-28">
			<view class="flex align-center pageBgColor h-px-40 pad-x-10 border-rad-3">
				<view class="flex mar-r-10">
					<text>+86</text>
					<u-icon name="arrow-down-fill" size="12" style="margin: 3px 0 0 3px;"></u-icon>
				</view>
				
				<u--input v-model="phone" type="number" maxlength="11" placeholder="请输入手机号码" border="none" clearable />
			</view>
			
			<view class="mar-t-10 font-14 color-cccccc">
				<text>注册过的手机号验证后自动登录</text>
			</view>
			
			<view class="mar-t-40">
				<u-button v-if="phone" type="primary" @click="getSMSCode()">获取短信验证码</u-button>
				<u-button v-else disabled color="#cccccc">获取短信验证码</u-button>
			</view>
		</view>
		
		<view class="pos-a font-14 color-999999" style="bottom: 30px; left: 28px;">
			<text>登录即表明同意</text>
			<text class="mainBlueColor">《售后宝用户协议》</text>
			<text>和</text>
			<text class="mainBlueColor">《隐私政策》</text>
		</view>
	</view>
</template>

<script>
	import Utils from '@/utils/utils'
	import { navigateTo } from '@/utils/router'
	import { checkPhone, checkLogin } from '@/utils/lib'
	import { register } from '@/apis/user'
	
	export default {
		data() {
			return {
				// 手机号
				phone: '', // 测试号码 17355110430
			}
		},
		
		onShow() {
			checkLogin()
		},
		
		methods: {
			// 获取短信验证码
			async getSMSCode() {
				// 校验手机号是否合法
				if (!checkPhone(this.phone)) {
					this.phone = ''
					Utils.popUpMsg('error', '手机号不合法', 1500)
					return
				}
				
				Utils.loading('正在发送验证码...')
				let systemInfo = await uni.getSystemInfo()
				// 检测手机号是否存在，获取短信验证码
				this.$store.dispatch('user/getSMSCode', this.phone).then(res => {
					uni.hideLoading()
					Utils.popUpMsg('success', res.Message, 500)
					
					// 短信发送成功，跳转到输入验证码页面
					setTimeout(() => {
						this.$store.commit('user/setMobile', this.phone)
						
						uni.navigateTo({
							url: '/pages/captch/captch'
						})
					}, 500)
				}).catch(err => {
					uni.hideLoading()
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import "@/static/scss/variable.module.scss";
	
	.pages {
		position: absolute;
		left: 0;
		right: 0;
		top: 0;
		bottom: 0;
		background-color: #fff;
	}
	
	.pageBgColor {
		background-color: $pageBgColor;
	}
	
	.mainBlueColor {
		color: $mainBlueColor;
	}
</style>
